package com.huang.mvvm.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.WindowManager
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.KeyboardUtils
import com.gyf.immersionbar.ImmersionBar
import com.huang.mvvm.common.ActivityManager
import com.huang.mvvm.R
import com.huang.mvvm.control.ControlProvider
import com.huang.core.util.KeyboardUtil
import com.huang.mvvm.vm.base.BaseActivityViewModel
import com.noober.background.BackgroundLibrary
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator
import org.greenrobot.eventbus.EventBus

abstract class BaseActivity<V : ViewDataBinding, VM : BaseActivityViewModel<*>> :
    BaseMvvMActivity<V, VM>() {

    var isAutoHideKeyBoard = true
    protected lateinit var controllerProvider: ControlProvider

    override fun onCreate(savedInstanceState: Bundle?) {
        BackgroundLibrary.inject(this)
        super.onCreate(savedInstanceState)
        ActivityManager.getManager().addActivity(this)
        getImmersionBar()?.init()
        ARouter.getInstance().inject(this)
        controllerProvider = ControlProvider(this)
        fragmentAnimator = DefaultHorizontalAnimator()
        initParams()
        initViewModelParams()
        initView()
        viewModel.initEvent()
        viewModel.initData()
        if (isRegisterEventBus()) EventBus.getDefault().register(this)
    }

    override fun onCreatedViewModel() {
        super.onCreatedViewModel()
        initDaggerInject()
    }

    override fun onBeforeObservable() {
        super.onBeforeObservable()
    }

    override fun onDestroyController() {
        controllerProvider.get().onDestroy()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        ActivityManager.getManager().finishActivity(this)
    }

    override fun finish() {
        KeyboardUtils.hideSoftInput(this)
        super.finish()
        overridePendingTransition(R.anim.anim_no, R.anim.anim_to_right_close)
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (isAutoHideKeyBoard && ev.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (KeyboardUtil.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(v!!)
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    //使onActivityResult能够传到fragment
    @SuppressLint("RestrictedApi")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        onActivityResultForFragment(supportFragmentManager.fragments, requestCode, resultCode, data)
    }

    @SuppressLint("RestrictedApi")
    private fun onActivityResultForFragment(
        rootFragmentList: List<Fragment>?,
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        if (rootFragmentList != null) {
            for (fragment in rootFragmentList) {
                if (fragment == null) continue
                fragment.onActivityResult(requestCode, resultCode, data)
                onActivityResultForFragment(
                    fragment.childFragmentManager.fragments,
                    requestCode,
                    resultCode,
                    data
                )
            }
        }
    }

    override fun showLoading(
        isCancelEnable: Boolean,
        isBackEnable: Boolean,
        onCanceledListener: (() -> Unit)?
    ) {
        controllerProvider.get().showLoading(this,isCancelEnable, isBackEnable, onCanceledListener)
    }

    override fun hideLoading() {
        controllerProvider.get().hideLoading()
    }

    override fun showMessage(text: String) {
        controllerProvider.get().showMessage(text)
    }

    override fun getContext(): Context? {
        return this
    }

    fun getActivity(): BaseActivity<V, VM> {
        return this
    }

    /** 是否注册EventBus */
    open fun isRegisterEventBus(): Boolean {
        return false
    }

    /** 设置状态栏效果  */
    open fun getImmersionBar(): ImmersionBar? {
        return ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
            .navigationBarDarkIcon(true)
            .navigationBarColor(android.R.color.white)
    }

    /** 初始化页面参数  */
    open fun initParams() {

    }

    /** Dagger注入 */
    open fun initDaggerInject(){

    }

    /** 初始化或传递ViewModel的参数  */
    open fun initViewModelParams() {

    }

    /** 初始化视图  */
    open fun initView() {

    }


}